Imports System
Imports MindFusion.Diagramming
Imports MindFusion.Drawing
Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.Utils
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraTreeList.Columns
Imports DevExpress.XtraTreeList

Public Class UcnPropertiesOfItem
    'represents PopupContainerControl for setting properties of node (diagram item)

#Region "Attributes"
#Region "Private attributes"
    Private MucnBrushPopupControl As UcnBrushPopupControl 'represents brush PopupContainerControl for PopupContainerEdit
    Private MucnPenPopupControl As UcnPenPopupControl 'represents pen PopupContainerControl for PopupContainerEdit
#End Region
#End Region
#Region "Enums"
    'types of properties of items (tables, links,...) in Properties TreeList 
    'C_ = Container; T_ = Table; L_ = Link; S_ = ShapeNode
    Public Enum GeTypeOfPropertyOfItem
        kkT_GroupAppearanceSettings = 0
        kkT_CaptionColor = 1
        kkT_RowsColor = 2
        kkT_Brush = 3

        kkC_GroupAppearanceSettings = 100
        kkC_CaptionColor = 101
        kkC_Brush = 102

        kkL_GroupAppearanceSettings = 200
        kkL_CaptionColor = 201
        kkL_Pen = 202

        kkS_Brush = 301
    End Enum
#End Region
#Region "Events"
    Public Event MMValueChanged(ByVal TypeOfPropertyOfItem As GeTypeOfPropertyOfItem, ByVal Value As Object) 'event is raised after value of cell is changed
#End Region
#Region "Constructors"
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        MucnBrushPopupControl = New UcnBrushPopupControl(New LinearGradientBrush(Color.Black, Color.Black, 100))
        MucnPenPopupControl = New UcnPenPopupControl(New Pen(Color.Black, 1))
        'ClearDisableTreelist()
    End Sub
#End Region
#Region "Events handler"
#Region "tlPropertiesOfItem - Events handler"
    'assigning right repositary control to a cell. Raised also for showing cells value not only before editing!
    Private Sub tlPropertiesOfItem_CustomNodeCellEdit(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.GetCustomNodeCellEditEventArgs) Handles tlPropertiesOfItem.CustomNodeCellEdit
        'Debug.Print("tlProperties_CustomNodeCellEdit")

        If e.Column.FieldName = "PropertyValue" Then
            'Debug.Print(GetTypeOfProperty(e.Node).ToString)
            Select Case GetTypeOfProperty(e.Node)
                Case GeTypeOfPropertyOfItem.kkT_CaptionColor, GeTypeOfPropertyOfItem.kkL_CaptionColor, GeTypeOfPropertyOfItem.kkT_RowsColor, GeTypeOfPropertyOfItem.kkC_CaptionColor
                    e.RepositoryItem = RepositoryItemColor
                Case GeTypeOfPropertyOfItem.kkT_Brush, GeTypeOfPropertyOfItem.kkC_Brush, GeTypeOfPropertyOfItem.kkS_Brush
                    RepositoryItemBrush.PopupControl = MucnBrushPopupControl.PopupContainerControl
                    e.RepositoryItem = RepositoryItemBrush
                Case GeTypeOfPropertyOfItem.kkL_Pen
                    RepositoryItemPen.PopupControl = MucnPenPopupControl.PopupContainerControl
                    e.RepositoryItem = RepositoryItemPen
                Case Else
                    Debug.Print("CHYBA!!! - tlProperties_CustomNodeCellEdit")
            End Select
        Else
            'e.RepositoryItem = Nothing
        End If
    End Sub
    'filling controls for editing with right data before entering to edit a cell
    Private Sub tlPropertiesOfItem_ShowingEditor(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles tlPropertiesOfItem.ShowingEditor
        Debug.Print("tlProperties_ShowingEditor")

        Dim LoFocusedColumn As TreeListColumn = Me.tlPropertiesOfItem.FocusedColumn
        Dim LoFocusedNode As TreeListNode = Me.tlPropertiesOfItem.FocusedNode

        'column for value of any property
        If (LoFocusedColumn.FieldName = "PropertyValue") Then
            'which property?
            Select Case GetTypeOfProperty(LoFocusedNode)
                Case GeTypeOfPropertyOfItem.kkT_Brush, GeTypeOfPropertyOfItem.kkC_Brush, GeTypeOfPropertyOfItem.kkS_Brush
                    Dim LoLinearGradientBrush As LinearGradientBrush = CType(LoFocusedNode.GetValue(LoFocusedColumn), LinearGradientBrush)
                    MucnBrushPopupControl.Init(LoLinearGradientBrush)
                Case GeTypeOfPropertyOfItem.kkL_Pen
                    Dim LoPen As Pen = CType(LoFocusedNode.GetValue(LoFocusedColumn), Pen)
                    MucnPenPopupControl.Init(LoPen)
            End Select
        End If
    End Sub
    'accepting changes after editing a cell
    Private Sub tlPropertiesOfItem_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.CellValueChangedEventArgs) Handles tlPropertiesOfItem.CellValueChanged
        Debug.Print("tlPropertiesOfItem_CellValueChanged")
        RaiseEvent MMValueChanged(GetTypeOfProperty(e.Node), e.Value)
    End Sub
#End Region
#Region "RepositoryItems - Events handler"
#Region "RepositoryItemBrush"
    'perform forced confirmation of changes of cell
    Private Sub RepositoryItemBrush_Closed(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ClosedEventArgs) Handles RepositoryItemBrush.Closed
        ForcedConfirmationOfChange()
    End Sub
    'setting a display value
    Private Sub RepositoryItemBrush_QueryDisplayText(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.QueryDisplayTextEventArgs) Handles RepositoryItemBrush.QueryDisplayText
        'Debug.Print("RepositoryItemBrush_QueryDisplayText")
        If e.EditValue IsNot Nothing Then
            Dim LoLinearGradientBrush As LinearGradientBrush = CType(e.EditValue, LinearGradientBrush)
            e.DisplayText = GetDescriptionOfLinearGradientBrush(LoLinearGradientBrush)
        Else
            Debug.Print("CHYBA!!! - RepositoryItemBrush_QueryDisplayText - e.EditValue = Nothing")
        End If
    End Sub
    'setting a result value
    Private Sub RepositoryItemBrush_QueryResultValue(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.QueryResultValueEventArgs) Handles RepositoryItemBrush.QueryResultValue
        Debug.Print("RepositoryItemBrush_QueryResultValue")
        e.Value = MucnBrushPopupControl.Result
    End Sub
#End Region
#Region "RepositoryItemPen"
    'perform forced confirmation of changes of cell
    Private Sub RepositoryItemPen_Closed(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ClosedEventArgs) Handles RepositoryItemPen.Closed
        ForcedConfirmationOfChange()
    End Sub
    'setting a display value
    Private Sub RepositoryItemPen_QueryDisplayText(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.QueryDisplayTextEventArgs) Handles RepositoryItemPen.QueryDisplayText
        Debug.Print("RepositoryItemPen_QueryDisplayText")
        If e.EditValue IsNot Nothing Then
            Dim LoPen As Pen = CType(e.EditValue, Pen)
            e.DisplayText = GetDescriptionOfPen(LoPen)
        Else
            Debug.Print("CHYBA!!! - RepositoryItemPen_QueryDisplayText - e.EditValue = Nothing")
        End If
    End Sub
    'setting a result value
    Private Sub RepositoryItemPen_QueryResultValue(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.QueryResultValueEventArgs) Handles RepositoryItemPen.QueryResultValue
        Debug.Print("RepositoryItemPen_QueryResultValue")
        e.Value = MucnPenPopupControl.Result
    End Sub
#End Region
#Region "RepositoryItemColor"
    'perform forced confirmation of changes of cell
    Private Sub RepositoryItemColor_Closed(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ClosedEventArgs) Handles RepositoryItemColor.Closed
        ForcedConfirmationOfChange()
    End Sub
#End Region
#End Region
#End Region
#Region "Subs"
    'initialization of controls
    Public Sub Init(ByVal IoActualItem As DiagramItem)
        ClearDisableData()

        If IoActualItem IsNot Nothing Then
            Select Case IoActualItem.GetType.Name
                ' '' ''Case "ClsMMTableNode"
                ' '' ''    MucnBrushPopupControl.Init(IoActualItem.Brush)
                Case "ClsMMLinkNode"
                    MucnPenPopupControl.Init(IoActualItem.Pen)
                    ' '' ''Case "ClsMMContainerNode"
                    ' '' ''    MucnBrushPopupControl.Init(IoActualItem.Brush)
                Case "ClsMMShapeNode"
                    MucnBrushPopupControl.Init(IoActualItem.Brush)
                Case Else
                    Debug.Print("CHYBA!!! - Init")
            End Select
        Else
            MucnBrushPopupControl.Init(Nothing)
            MucnPenPopupControl.Init(Nothing)
        End If

        RefreshPropertiesOfItem(IoActualItem)
    End Sub
    'clear datasource
    Public Sub Clear()
        Init(Nothing)
    End Sub
    'clear treelist and disable it
    Private Sub ClearDisableData()
        Me.tlPropertiesOfItem.Enabled = False
        Me.tlPropertiesOfItem.BeginUpdate()
        Me.tlPropertiesOfItem.DataSource = Nothing 'clearing datasource
        Me.tlPropertiesOfItem.ClearNodes() 'clearing nodes
        Me.tlPropertiesOfItem.EndUpdate()
        MucnBrushPopupControl.Init(Nothing)
        MucnPenPopupControl.Init(Nothing)
    End Sub
    'forced confirmation of changes of cell in tlProperties in treelist
    Private Sub ForcedConfirmationOfChange()
        Me.tlPropertiesOfItem.FocusedColumn = Me.tlPropertiesOfItem.Columns("PropertyName")
        'now tlProperties_CellValueChanged event is raised - thats what I want!
        'and now set focus back to editable column
        Me.tlPropertiesOfItem.FocusedColumn = Me.tlPropertiesOfItem.Columns("PropertyValue")
    End Sub
    'refreshing properties of item
    Private Sub RefreshPropertiesOfItem(ByVal IoActualItem As DiagramItem)
        If IoActualItem IsNot Nothing Then
            Select Case IoActualItem.GetType.Name
                ' '' ''Case "ClsMMTableNode"
                ' '' ''    Dim LoMMTableNode As ClsMMTableNode = CType(IoActualItem, ClsMMTableNode)
                ' '' ''    Dim LoProperties As ClsTreeListRowOfTreeListProperties() = New ClsTreeListRowOfTreeListProperties(2) {}

                ' '' ''    'LoProperties(0) = New ClsTreeListRowOfTreeListProperties("Vzhlad", Nothing, GeTypeOfPropertyOfItem.kkT_GroupAppearanceSettings, 0)
                ' '' ''    LoProperties(0) = New ClsTreeListRowOfTreeListProperties("Farba pisma zahlavia", LoMMTableNode.CaptionColor, GeTypeOfPropertyOfItem.kkT_CaptionColor, 0)
                ' '' ''    LoProperties(1) = New ClsTreeListRowOfTreeListProperties("Farba pisma riadkov", LoMMTableNode.TextColor, GeTypeOfPropertyOfItem.kkT_RowsColor, 1)
                ' '' ''    LoProperties(2) = New ClsTreeListRowOfTreeListProperties("Vypln", LoMMTableNode.Brush, GeTypeOfPropertyOfItem.kkT_Brush, 2)

                ' '' ''    Me.tlPropertiesOfItem.BeginUpdate()
                ' '' ''    Me.tlPropertiesOfItem.DataSource = LoProperties
                ' '' ''    'setting order of columns. Only for sure because order should be ok now - order is same as order of properties in class ClsTreeListRow
                ' '' ''    Me.tlPropertiesOfItem.Columns("PropertyName").VisibleIndex = 0
                ' '' ''    Me.tlPropertiesOfItem.Columns("PropertyValue").VisibleIndex = 1
                ' '' ''    Me.tlPropertiesOfItem.Columns("PropertyID").VisibleIndex = -1 'invisible column
                ' '' ''    'setting editable for columns
                ' '' ''    Me.tlPropertiesOfItem.Columns("PropertyName").OptionsColumn.AllowEdit = False
                ' '' ''    '!!! RowID and ParentRowID PROPERTIES ARE AUTOMATICALLY INVISIBLE
                ' '' ''    '!!! SEE KeyFieldName and ParentFieldName PROPERTIES OF TREELIST
                ' '' ''    '!!! Me.tlProperties.Columns("RowID").VisibleIndex = -1 'invisible column
                ' '' ''    '!!! Me.tlProperties.Columns("ParentRowID").VisibleIndex = -1 'invisible column

                ' '' ''    Me.tlPropertiesOfItem.BestFitColumns()
                ' '' ''    Me.tlPropertiesOfItem.ExpandAll()
                ' '' ''    Me.tlPropertiesOfItem.EndUpdate()
                ' '' ''    If Me.tlPropertiesOfItem.Enabled = False Then Me.tlPropertiesOfItem.Enabled = True

                Case "ClsMMLinkNode"
                    Dim LoMMLinkNode As ClsMMLinkNode = CType(IoActualItem, ClsMMLinkNode)
                    Dim LoProperties As ClsTreeListRowOfTreeListProperties() = New ClsTreeListRowOfTreeListProperties(1) {}

                    'LoProperties(0) = New ClsTreeListRowOfTreeListProperties("Vzhlad", Nothing, GeTypeOfPropertyOfItem.kkL_GroupAppearanceSettings, 0)
                    LoProperties(0) = New ClsTreeListRowOfTreeListProperties("Farba pisma nazvu", LoMMLinkNode.TextColor, GeTypeOfPropertyOfItem.kkL_CaptionColor, 0)
                    LoProperties(1) = New ClsTreeListRowOfTreeListProperties("Ciara", LoMMLinkNode.Pen, GeTypeOfPropertyOfItem.kkL_Pen, 1)

                    Me.tlPropertiesOfItem.BeginUpdate()
                    Me.tlPropertiesOfItem.DataSource = LoProperties
                    'setting order of columns. Only for sure because order should be ok now - order is same as order of properties in class ClsTreeListRowOfTLProperties
                    Me.tlPropertiesOfItem.Columns("PropertyName").VisibleIndex = 0
                    Me.tlPropertiesOfItem.Columns("PropertyValue").VisibleIndex = 1
                    Me.tlPropertiesOfItem.Columns("PropertyID").VisibleIndex = -1 'invisible column
                    '!!! RowID and ParentRowID PROPERTIES ARE AUTOMATICALLY INVISIBLE
                    '!!! SEE KeyFieldName and ParentFieldName PROPERTIES OF TREELIST
                    '!!! Me.tlProperties.Columns("RowID").VisibleIndex = -1 'invisible column
                    '!!! Me.tlProperties.Columns("ParentRowID").VisibleIndex = -1 'invisible column

                    Me.tlPropertiesOfItem.BestFitColumns()
                    Me.tlPropertiesOfItem.ExpandAll()
                    Me.tlPropertiesOfItem.EndUpdate()
                    If Me.tlPropertiesOfItem.Enabled = False Then Me.tlPropertiesOfItem.Enabled = True

                    ' '' ''Case "ClsMMContainerNode"
                    ' '' ''    Dim LoMMContainerNode As ClsMMContainerNode = CType(IoActualItem, ClsMMContainerNode)
                    ' '' ''    Dim LoProperties As ClsTreeListRowOfTreeListProperties() = New ClsTreeListRowOfTreeListProperties(1) {}

                    ' '' ''    'LoProperties(0) = New ClsTreeListRowOfTreeListProperties("Vzhlad", Nothing, GeTypeOfPropertyOfItem.kkC_GroupAppearanceSettings, 0)
                    ' '' ''    LoProperties(0) = New ClsTreeListRowOfTreeListProperties("Farba pisma zahlavia", LoMMContainerNode.CaptionColor, GeTypeOfPropertyOfItem.kkC_CaptionColor, 0)
                    ' '' ''    LoProperties(1) = New ClsTreeListRowOfTreeListProperties("Vypln", LoMMContainerNode.Brush, GeTypeOfPropertyOfItem.kkC_Brush, 1)

                    ' '' ''    Me.tlPropertiesOfItem.BeginUpdate()
                    ' '' ''    Me.tlPropertiesOfItem.DataSource = LoProperties
                    ' '' ''    'setting order of columns. Only for sure because order should be ok now - order is same as order of properties in class ClsTreeListRow
                    ' '' ''    Me.tlPropertiesOfItem.Columns("PropertyName").VisibleIndex = 0
                    ' '' ''    Me.tlPropertiesOfItem.Columns("PropertyValue").VisibleIndex = 1
                    ' '' ''    Me.tlPropertiesOfItem.Columns("PropertyID").VisibleIndex = -1 'invisible column
                    ' '' ''    'setting editable for columns
                    ' '' ''    Me.tlPropertiesOfItem.Columns("PropertyName").OptionsColumn.AllowEdit = False
                    ' '' ''    '!!! RowID and ParentRowID PROPERTIES ARE AUTOMATICALLY INVISIBLE
                    ' '' ''    '!!! SEE KeyFieldName and ParentFieldName PROPERTIES OF TREELIST
                    ' '' ''    '!!! Me.tlProperties.Columns("RowID").VisibleIndex = -1 'invisible column
                    ' '' ''    '!!! Me.tlProperties.Columns("ParentRowID").VisibleIndex = -1 'invisible column

                    ' '' ''    Me.tlPropertiesOfItem.BestFitColumns()
                    ' '' ''    Me.tlPropertiesOfItem.ExpandAll()
                    ' '' ''    Me.tlPropertiesOfItem.EndUpdate()
                    ' '' ''    If Me.tlPropertiesOfItem.Enabled = False Then Me.tlPropertiesOfItem.Enabled = True

                Case "ClsMMShapeNode"
                    Dim LoMMShapeNode As ClsMMShapeNode = CType(IoActualItem, ClsMMShapeNode)
                    Dim LoProperties As ClsTreeListRowOfTreeListProperties() = New ClsTreeListRowOfTreeListProperties(0) {}

                    LoProperties(0) = New ClsTreeListRowOfTreeListProperties("Vypln", LoMMShapeNode.Brush, GeTypeOfPropertyOfItem.kkS_Brush, 2)

                    Me.tlPropertiesOfItem.BeginUpdate()
                    Me.tlPropertiesOfItem.DataSource = LoProperties
                    'setting order of columns. Only for sure because order should be ok now - order is same as order of properties in class ClsTreeListRow
                    Me.tlPropertiesOfItem.Columns("PropertyName").VisibleIndex = 0
                    Me.tlPropertiesOfItem.Columns("PropertyValue").VisibleIndex = 1
                    Me.tlPropertiesOfItem.Columns("PropertyID").VisibleIndex = -1 'invisible column
                    'setting editable for columns
                    Me.tlPropertiesOfItem.Columns("PropertyName").OptionsColumn.AllowEdit = False
                    '!!! RowID and ParentRowID PROPERTIES ARE AUTOMATICALLY INVISIBLE
                    '!!! SEE KeyFieldName and ParentFieldName PROPERTIES OF TREELIST
                    '!!! Me.tlProperties.Columns("RowID").VisibleIndex = -1 'invisible column
                    '!!! Me.tlProperties.Columns("ParentRowID").VisibleIndex = -1 'invisible column

                    Me.tlPropertiesOfItem.BestFitColumns()
                    Me.tlPropertiesOfItem.ExpandAll()
                    Me.tlPropertiesOfItem.EndUpdate()
                    If Me.tlPropertiesOfItem.Enabled = False Then Me.tlPropertiesOfItem.Enabled = True

                Case Else
                    Debug.Print("CHYBA!!! - RefreshPropertiesOfItem")
            End Select
        End If
    End Sub
#End Region
#Region "Functions"
    'returns type of property assigned to this node in treelist
    Private Function GetTypeOfProperty(ByVal IoNode As TreeListNode) As GeTypeOfPropertyOfItem
        Return IoNode.GetValue("PropertyID")
    End Function
#End Region

End Class
